package com.easemob.helpdesk.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.helpdesk.IMHelper;
import com.easemob.helpdesk.listener.OnDataItemClickListener;
import com.housekeeperapp.R;
import com.easemob.helpdesk.activity.SearchWaitAccessActivity;
import com.easemob.helpdesk.activity.MainActivity;
import com.hyphenate.kefusdk.chat.HDClient;
//import com.hyphenate.kefusdk.entity.HDUser;
import com.hyphenate.kefusdk.entity.user.HDUser;
import com.hyphenate.kefusdk.gsonmodel.main.WaitQueueResponse;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Created by liyuzhao on 16/3/7.
 */
public class WaitAccessAdapter extends RecyclerArrayAdapter<WaitQueueResponse.ItemsBean> {
    private static final String TAG = "WaitAccessAdapter";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public WaitAccessAdapter(Context context){
        super(context);
    }

    @Override
    public BaseViewHolder onCreateHolder(ViewGroup viewGroup, int viewType) {
        return new WaitAccessHolder(viewGroup);
    }

    private OnDataItemClickListener<WaitQueueResponse.ItemsBean> dataItemClickListener;

    public void setOnDataItemClickListener(OnDataItemClickListener listener) {
        this.dataItemClickListener = listener;
    }


    class WaitAccessHolder extends BaseViewHolder<WaitQueueResponse.ItemsBean> {
        private TextView tvName;
        private TextView tvTime;
        private ImageView vipIcon;
//        private TextView tvVip;
//        private View jieRu;
//        private View transfer;
//        private View close;

        public WaitAccessHolder(ViewGroup parent) {
            super(parent, R.layout.row_waiting_access);
            tvName = $(R.id.tv_name);
            tvTime = $(R.id.tv_time);
            vipIcon = $(R.id.vip_icon);
//            tvVip = $(R.id.tv_vip);
//            jieRu = $(R.id.rl_jieru);
//            transfer = $(R.id.rl_transfer);
//            close = $(R.id.rl_close);

        }

        @Override
        public void setData(final WaitQueueResponse.ItemsBean data) {
            super.setData(data);
            if (data == null) {
                return;
            }
            tvName.setText(data.getVisitorName());
            String createdAt = data.getCreatedAt();
            try {
                String t = createdAt.substring(0, createdAt.indexOf(".")).replace("T", " ");
                Log.d(TAG, "setData: "+t);
                tvTime.setText(t);
            } catch (Exception e) {
                tvTime.setText(createdAt);
            }
//            tvVip.setText(data.isVip() ? "是" : "否");

            HDUser emUser = HDClient.getInstance().getCurrentUser();
            if (emUser != null && emUser.getRoles() != null){
                String roles = emUser.getRoles();
                if (roles.contains("admin")){
//                    transfer.setVisibility(View.VISIBLE);
//                    close.setVisibility(View.VISIBLE);
                }else{
//                    transfer.setVisibility(View.INVISIBLE);
//                    close.setVisibility(View.INVISIBLE);
                }

            }

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dataItemClickListener != null) {
                        dataItemClickListener.onClick(v, data);
                    }
                }
            });

//            jieRu.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(getContext() instanceof MainActivity){
//                        ((MainActivity)getContext()).getMsgFragment().waitIntent(getAdapterPosition(), 1);
//                    }else if (getContext() instanceof SearchWaitAccessActivity){
//                        ((SearchWaitAccessActivity)getContext()).clickAccess(getAdapterPosition(), 1);
//                    }
//                }
//            });
//
//
//            transfer.setOnClickListener(new View.OnClickListener(){
//
//                @Override
//                public void onClick(View v) {
//                    if(getContext() instanceof MainActivity){
//                        ((MainActivity)getContext()).getMsgFragment().waitIntent(getAdapterPosition(), 2);
//                    }else if (getContext() instanceof SearchWaitAccessActivity){
//                        ((SearchWaitAccessActivity)getContext()).clickAccess(getAdapterPosition(), 2);
//                    }
//                }
//            });
//
//            close.setOnClickListener(new View.OnClickListener(){
//
//                @Override
//                public void onClick(View v) {
//                    if(getContext() instanceof MainActivity){
//                        ((MainActivity)getContext()).getMsgFragment().waitIntent(getAdapterPosition(), 3);
//                    }else if (getContext() instanceof SearchWaitAccessActivity){
//                        ((SearchWaitAccessActivity)getContext()).clickAccess(getAdapterPosition(), 3);
//                    }
//                }
//            });

        }
    }



}
